﻿<%@ Page Title="" Language="C#" MasterPageFile="~/User/User.Master" AutoEventWireup="true"
    CodeBehind="SK.aspx.cs" Inherits="CoolERP_Web.User.SK" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script src="../Scripts/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
<script src="../Scripts/lhgdialog/lhgcore.min.js" type="text/javascript"></script>
<script src="../Scripts/lhgdialog/lhgdialog.js" type="text/javascript"></script>
<script type="text/javascript">
    var count = 1;
    var old_count = "";
    $(function () {
        $("[id='txtCount']").keydown(function (event) {
            old_count = $(this).val();
            var k = event.keyCode;
            if ((k <= 57 && k >= 48) || (k <= 105 && k >= 96) || (k == 8))
                return true;
            else {
                $(this).val(old_count);
                return false;
            }
        });
    });
    function setChange(obj) {
        var count = parseFloat($(obj).val());
        var ysze = $(obj).closest("tr").find("td").eq(4).text();
        var yishouze = $(obj).closest("tr").find("td").eq(5).text();
        if (count > (parseFloat(ysze) - parseFloat(yishouze)) || count < 0) {
            alert('分配金额必须在允许范围内:大于0且小于前两列之差');
             }
          setTotal();
    }
    function setTotal() {
        var totalMoney = 0;
        $("#tbGoodsInfo tr").each(function (i) {
            if (i > 0) {
                var txtFPJE = $(this).find("[id='txtFPJE']").val();
                if (!isEmpty(txtFPJE))
                    totalMoney += parseFloat(txtFPJE);
            }
        });
        $("#spanMoney").text(totalMoney.toFixed(2));
        $("#divTotalMoney").show();
    }
    function resetXH() {
        $("#tbGoodsInfo tr").each(function (i) {
            if (i > 0)
                $(this).find("td").eq(0).text(i);
        });
    }
    function setHid() {
        var ids = "";
        $("#tbGoodsInfo input[type=hidden]").each(function () {
            ids += $(this).attr("value") + ",";
        });
        if (ids != "")
            ids = ids.substr(0, ids.length - 1);
        $("#hidSelIds").val(ids);
    }
    function DelGoods(obj) {
        $(obj).parent().parent().remove();
        resetXH();
        setHid();
        setTotal();
    }
    var dg;
    function selGoods() {
        if (isEmpty($("#hidKHID").val())) {
            alert("请选择客户");
        }
        else {
            dg = new J.dialog({ id: 'select', title: '选择应收款帐单', page: 'SelectYSKZD.aspx?id=' + $("#hidKHID").val(), iconTitle: false, cover: true, btnBar: false, rang: true, autoSize: true, width: 900, height: 650 });
            dg.ShowDialog();
        }
    }
    function selCustomer() {
        dg = new J.dialog({ id: 'select', title: '选择客户', page: 'SelectCustomer.aspx', iconTitle: false, cover: true, btnBar: false, rang: true, autoSize: true, width: 900, height: 650 });
        dg.ShowDialog();
    }
    function selMore(goodsList) {
        resetXH();
        for (var i = 0; i < goodsList.length; i++) {
            var row = $("#tbGoodsInfo tr").length;
            var id = goodsList[i].id;
            var bh = goodsList[i].bh;
            var xshtbh = goodsList[i].xshtbh;
            var czryname = goodsList[i].czryname;
            var ysze = goodsList[i].ysze;
            var yishouze = goodsList[i].yishouze;

            $("<tr><td>" + row + "</td><td>" + bh + "<input type=hidden id='hidGid' value='" + id + "' /></td><td>" + xshtbh + "</td><td>" + czryname + "</td><td>" + ysze + "</td><td>" + yishouze + "</td>\
                <td><input type=text class='textbox' id='txtFPJE' value='' style='width:60px; text-align:center' onchange='setChange(this)' /></td>\
                <td><img src='../Images/Common/btn_del0.png' onclick='DelGoods(this)' style='cursor:pointer' /></td></tr>").appendTo("#tbGoodsInfo");
        }
        setHid();
        dg.cancel();
    }
    function checkSubmit() {

        if (isEmpty($("#hidKHID").val())) {
            alert("请选择客户");
            return false;
        }
        if (isEmpty($("#tbZPHM").val())) {
            alert("请填写支票号码");
            return false;
        }
        if (isEmpty($("#tbSSJE").val())) {
            alert("请填写实收金额");
            return false;
        }
        var isOk = true;
        var idList = new Array();
        var fpjeList = new Array();
        $("#tbGoodsInfo tr").each(function (i, item) {
            if (i > 0) {
                var goods = {
                    id: $(this).find("input[type=hidden]").val(),
                    bh: $(this).find("td").eq(1).val(),
                    fpje: $(this).find("input[type=text]").eq(0).val()
                };
                if (!isEmpty(goods.id) && isEmpty(goods.fpje)) {
                    alert("请填写第" + i + "个应收款帐单的分配金额");
                    isOk = false;
                    return isOk;
                }
                if (idList.contains(goods.id)) {
                    alert("有两个相同的产品信息：" + goods.name);
                    isOk = false;
                    return isOk;
                }
                idList[idList.length] = goods.id;
                fpjeList[fpjeList.length] = goods.fpje;
            }
        });
        if (!isOk)
            return false;
        var totalMoney = 0;
        $("#tbGoodsInfo tr").each(function (i) {
            if (i > 0) {
                var txt_fpje = $(this).find("[id='txtFPJE']").val();
                if ( !isEmpty(txt_fpje))
                    totalMoney += parseFloat(txt_fpje);
            }
        });
        if (parseFloat($("#tbSSJE").val()) != totalMoney) {
            alert("你所分配的金额总数不等于实收总额，请确认无误后再保存");
            return false;
        }
        $("#hidYSKZDID").val(idList);
        $("#hidFPJE").val(fpjeList);
        if (isEmpty($("#hidYSKZDID").val())) {
            alert("至少必须添加一个应收款帐单");
            return false;
        }

        return true;
    }
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td class=tbHead_L></td>
        <td class=tbHead_M><div><b><asp:Literal ID="liTitle" runat=server>应收款帐单</asp:Literal>收款</b></div></td>
        <td class=tbHead_R></td>
    </tr>
    <tr>
        <td class=tbBody_L></td>
        <td class=tbBody_M>
        <div style="width:100%;">
            <ul class=tags style="width:100%;">
                <li class=liTitle><div>收款单详细信息</div></li>
            </ul>
            <table class="grvCSS" cellspacing="0" cellpadding=2 border="1" style="width:100%;border-collapse:collapse;">
                <tr>
                    <td width=10%>客户简称：</td>
                    <td style="text-align:left;width:30%"><asp:TextBox ID="txtKH" runat="server" CssClass="textbox" MaxLength="16" ReadOnly=true></asp:TextBox>
                        <input type=button value="选择客户" class=btn_s onclick="selCustomer()" />
                        <asp:HiddenField ID="hidKHID" runat="server" /></td>
                    <td width=10%>收入银行：</td>
                    <td style="text-align:left;width:30%">
                        <asp:DropDownList ID="ddlSRYH" runat="server" CssClass="dropdown">
                        </asp:DropDownList>
                    </td>

                </tr>
                <tr>
                    <td width=10%>支票号码：</td>
                    <td style="text-align:left; width:30%" >
                        <asp:TextBox ID="tbZPHM" runat="server"></asp:TextBox></td>
                    <td width=10%>实收金额：</td>
                    <td style="text-align:left;width:30%">
                        <asp:TextBox ID="tbSSJE" runat="server" ></asp:TextBox></td>
                </tr>
            </table>
            <ul class=tags style="width:100%;">
                <li class=liTitle><div>收款单分配列表</div></li>
                <li class=liRight><img onclick="selGoods()" title="选择本次收款的应收款帐单" src="../Images/Common/btn_add0.png" style="cursor:pointer" /></li>
            </ul>
            <table id="tbGoodsInfo" class=grvCSS cellpadding=0 cellspacing=0 border=1 style="width:100%;border-collapse:collapse;">
                <tr>
                    <th>序号</th><th>应收款帐单编号</th><th>合同编号</th><th>操作人员</th><th>应收总额</th><th>已收总额</th><th>分配金额</th>
                </tr>            
            <asp:Repeater ID="repMXList" runat="server">
            <ItemTemplate>
                <tr>
                    <td><%# Container.ItemIndex + 1%></td>
                    <td><%# Eval("YSKZDBH") %><input type=hidden id="hidGid" value='<%# Eval("YSKZDID") %>' /></td>
                    <td><%# Eval("XSHTBH") %></td><td><%# Eval("CZRYName") %></td>
                    <td><%# Eval("YSZE") %></td><td><%# Eval("YISHOUZE") %></td>
                    <td><input type=text class="textbox" id="txtFPJE" value='<%# Eval("FPJE") %>' style="width:60px; text-align:center" onchange="setChange(this)" /></td>
                    <td><img src="../Images/Common/btn_del0.png" onclick="DelGoods(this)" style="cursor:pointer" /></td>
                </tr>
            </ItemTemplate>
            </asp:Repeater>
            </table>
            <asp:Panel ID="divTotalMoney" runat="server">
                总计金额：¥<asp:Label ID="spanMoney" runat="server" ForeColor=Red/>
            </asp:Panel>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
            <p><asp:ImageButton ID="btnSave" runat="server" ImageUrl="~/Images/Common/btn_save0.png" onclick="btnSave_Click" OnClientClick="return checkSubmit();" />&nbsp;&nbsp;
                <a href="YSKSK.aspx"><img src="../Images/Common/btn_back0.png" border=0 /></a>&nbsp;&nbsp;
                <asp:Label ID="labResult" runat="server" ForeColor=Red></asp:Label>
            </p></ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
            </Triggers>
            </asp:UpdatePanel>
        </div>
        </td>
        <td class=tbBody_R></td>
    </tr>
    <tr>
        <td class=tbFoot_L></td>
        <td class=tbFoot_M></td>
        <td class=tbFoot_R></td>
    </tr>
</table>
<asp:HiddenField ID="hidYSKZDID" runat="server" /><asp:HiddenField ID="hidPrices" runat="server" />
<asp:HiddenField ID="hidFPJE" runat="server" />
</asp:Content>
